package com.yuke.cloud.service.smc.service.impl;

import com.alibaba.fastjson.JSON;
import com.yuke.cloud.common.base.constant.GlobalConstant;
import com.yuke.cloud.common.base.dto.MsgNotificationBody;
import com.yuke.cloud.common.base.enums.ErrorCodeEnum;
import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.cmc.service.CmcFeignApi;
import com.yuke.cloud.service.api.opc.service.OpcSendNotifyFeignApi;
import com.yuke.cloud.service.api.pmc.exceptions.PmcBizException;
import com.yuke.cloud.service.api.pmc.service.PmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.ModifyUserTypeDto;
import com.yuke.cloud.service.api.uac.exceptions.UacBizException;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.smc.dao.ShopAuditLogMapper;
import com.yuke.cloud.service.smc.entity.Shop;
import com.yuke.cloud.service.smc.entity.ShopAuditLog;
import com.yuke.cloud.service.smc.entity.ShopGoods;
import com.yuke.cloud.service.smc.service.ShopAuditLogService;
import com.yuke.cloud.service.smc.service.ShopGoodsService;
import com.yuke.cloud.service.smc.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;


/**
 * Created by wg on 2018/09/08.
 */
@Slf4j
@Service
@Transactional
public class ShopAuditLogServiceImpl extends BaseService<ShopAuditLog> implements ShopAuditLogService {
    @Resource
    private ShopAuditLogMapper shopAuditLogMapper;
    @Resource
    private ShopService shopService;
    @Resource
    private ShopGoodsService shopGoodsService;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private OpcSendNotifyFeignApi opcSendNotifyFeignApi;
    @Resource
    private PmcFeignApi pmcFeignApi;

    @Resource
    private CmcFeignApi cmcFeignApi;

    @Override
    public int save(ShopAuditLog record) {
        log.info("--- Enter save(ShopAuditLog record)，参数：{} ---", record.getShopId());
        Long userId = shopService.selectByKey(record.getShopId()).getUserId();
        String title = "";
        String content = "";
        if (record != null && record.getAuditType() != null && record.getResult() != null) {
            if (record.getAuditType().equals((byte)1)) {
                //开店申请
                title= "店铺申请";
                Shop shop = new Shop();
                shop.setShopId(record.getShopId());
                switch (record.getResult()) {
                    case 0:
                        log.info("--- {}店铺审核未知 ---", record.getShopId());
                        shop.setCreateStatus((byte)0);
                        shopService.update(shop);
                        content = "很遗憾，由于未知原因，您的店铺被关闭，详情请联系客服处理";
                        break;
                    case 1:
                        //申请通过，成为商家
                        log.info("--- {}店铺审核通过 ---", record.getShopId());
                        shop.setCreateStatus((byte)2);
                        shop.setRunStatus((byte)1);  // add by wg 20190513 默认通过时店铺为运行状态
                        shop.setShopType(record.getShopType());//TODO 20200405 修改店铺类型
                        shopService.update(shop);
                        //调用feign，改变用户的类型为商家，并设置为商家
                        ModifyUserTypeDto modifyUserTypeDto1 = new ModifyUserTypeDto();
                        modifyUserTypeDto1.setUserId(userId);
                        modifyUserTypeDto1.setType(GlobalConstant.Sys.USER_TYPE_SHOPPER);
                        modifyUserTypeDto1.setIsSet(true);
                        uacUserFeignApi.modifyUserType(modifyUserTypeDto1);


                        content = "恭喜您，您的店铺申请已通过~";
                        //店铺注册成功发放优惠券
                        cmcFeignApi.addCouponReceive(shop.getShopId(),5,1);
                        break;
                    case 2:
                        //申请未通过
                        log.info("--- {}店铺审核未通过 ---", record.getShopId());
                        shop.setCreateStatus((byte)3);
                        shopService.update(shop);
                        content = "很遗憾，您的店铺申请未通过，请修改您的申请信息重新提交";
                        break;
                    case 3:
                        //平台强制关闭
                        log.info("--- {}店铺被平台强制关闭 ---", record.getShopId());
                        shop.setCreateStatus((byte)4);
                        shopService.update(shop);
                        ModifyUserTypeDto modifyUserTypeDto3 = new ModifyUserTypeDto();
                        modifyUserTypeDto3.setUserId(record.getAdminId());
                        modifyUserTypeDto3.setType(GlobalConstant.Sys.USER_TYPE_SHOPPER);
                        modifyUserTypeDto3.setIsSet(false);
                        Wrapper wrapper = uacUserFeignApi.modifyUserType(modifyUserTypeDto3);
                        if (wrapper.error()) {
                            log.error("--- uacUserFeignApi.modifyUserType(modifyUserTypeDto3)内部异常，详情：{} ---", wrapper.getMessage());
                            throw new UacBizException(ErrorCodeEnum.GL99990002);
                        }
                        content = "很遗憾，您的店铺被强制关闭，详情请联系客服处理";
                        break;
                }

            }
            if (record.getAuditType() == 2) {
                //商品申请
                title = "商品申请";
                String specName = "";
                ShopGoods shopGoods = new ShopGoods();
                shopGoods.setShopGoodsId(record.getShopGoodsId());
                Wrapper<String> wrapper = pmcFeignApi.getSpecNameBySpecId(shopGoodsService.selectByKey(record.getShopGoodsId()).getProductId());
                if (wrapper.error()) {
                    log.error("--- pmcFeignApi.getSpecNameBySpecId(Long specId)内部异常，详情：{} ---", wrapper.getMessage());
                    throw new PmcBizException(ErrorCodeEnum.GL99990002);
                }
                specName = wrapper.getResult();
                switch (record.getResult()) {
                    case 0:
                        log.info("--- {}商品审核未知 ---", record.getShopGoodsId());
                        shopGoods.setGoodsStatus((byte)0);
                        content = "很遗憾，由于未知原因，" + specName + "无法申请，详情请联系客服处理";
                        break;
                    case 1:
                        log.info("--- {}商品审核通过 ---", record.getShopGoodsId());
                        shopGoods.setGoodsStatus((byte)2);
                        content = "恭喜您，" + specName + "审核通过,您可以下单啦~";
                        break;
                    case 2:
                        log.info("--- {}商品审核未通过 ---", record.getShopGoodsId());
                        shopGoods.setGoodsStatus((byte)3);
                        content = "很遗憾，" + specName + "审核未通过，您不能经营该商品";
                        break;
                    case 3:
                        log.info("--- {}商品被平台禁止销售 ---", record.getShopGoodsId());
                        shopGoods.setGoodsStatus((byte)4);
                        content = "很遗憾，" + specName + "被平台禁止销售，申诉请联系客服处理";
                        break;
                }
                shopGoodsService.update(shopGoods);
            }
            if (record.getAuditType().equals((byte)5)) {
                //开团申请
                title= "开团申请";
                Shop shop = new Shop();
                shop.setShopId(record.getShopId());
                switch (record.getResult()) {
                    case 0:
                        log.info("--- {}开团审核未知 ---", record.getShopId());
                        content = "很遗憾，由于未知原因，无法申请，详情请联系客服处理";
                        break;
                    case 1:
                        log.info("--- {}开团审核通过 ---", record.getShopId());
                        //申请通过，成为商家

                        shop.setTeamStatus((byte)2);
                        shopService.update(shop);
                        content = "恭喜您，审核通过~";
                        break;
                    case 2:
                        //申请未通过
                        log.info("--- {}开团审核未通过,{} ---", record.getShopId(),record.getMessage());
                        shop.setTeamStatus((byte)0);
                        shopService.update(shop);
                        content = "很遗憾，您的开团申请未通过。"+ record.getMessage();
                        break;


                }

            }
        }
        record.setShopAuditId(generateId());
        int count = super.save(record);
        if (count > 0 ) {
            MsgNotificationBody msgNotificationBody = new MsgNotificationBody();
            msgNotificationBody.setReceiveType((byte)3);
            msgNotificationBody.setMsgType("shopMsg");
            msgNotificationBody.setTitle(title);
            msgNotificationBody.setContent(content);
            msgNotificationBody.setTime(new Date());
            opcSendNotifyFeignApi.sendnotify(userId.toString(), title, content, 0);
            opcSendNotifyFeignApi.sendnotify(userId.toString(), title, JSON.toJSON(msgNotificationBody).toString(), 1);
            //TODO:调用失败如何处理，待写
        }
        log.info("--- Leave save(ShopAuditLog record)，返回值：{} ---", count);
        return count;
    }
}
